<?php
/**
 * 扫描es 数据索引，更新geo字段，转换 魔卡坐标(米)为百度坐标
 */
header("content-type:text/plain;charset=utf8");
require './include/config.php';
require './include/CurlMulti.php';

// 存储数据

$index = 'baidu_xiaoqu2';
$type = 'xiaoqu';
$es_bulk = $conf['es']['hosts'].'/_bulk';
$es_url = $conf['es']['hosts'].'/'.$index.'/'.$type.'/_search';

try {
    
    $post = '{"size":1000,"_source":{"includes":["name","addr","diPointX","diPointY","baidu_geo"]},
     "query":{"bool":{"must":[
         {"range":{"diPointX":{"gt":1291223918,"lt":1305380716}}},
         {"range":{"diPointY":{"gt":479371047,"lt":489585446}}},
         {"wildcard":{"std_tag":"*房地产*"}}
    ]}}}';
    $res = fly_curl($conf['es']['hosts'] . '/' . $index . '/_search?search_type=scan&scroll=1m',$post) ;
    $res = json_decode($res, true);
    $count = 0;
    while ($page = fly_curl($conf['es']['hosts'] . '/_search/scroll?scroll=20s&&scroll_id=' . $res["_scroll_id"])) {
        $page = json_decode($page, true);
        $count += count($page["hits"]["hits"]);
        echo "读取 [",$index,"] 索引数据 ...", "数据读取 ",$count,"/",$page["hits"]["total"]," ... 进度 ",round($count/$page["hits"]["total"] * 100, 3),"%","\n";
        
        $coords = $body = '';
        foreach ($page["hits"]["hits"] as $item) {
            if (!isset($item["_source"]["baidu_geo"]) || !is_array($item["_source"]["baidu_geo"])) {
                $coord_key = ($item["_source"]["diPointX"]/100) .','. ($item["_source"]["diPointY"]/100);
                $coords[] = $coord_key;
            }
        }
        if ($coords) {
            $temp = geoconv($coords);
        }
        foreach ($page["hits"]["hits"] as $item) {
            $coord_key = ($item["_source"]["diPointX"]/100) .','. ($item["_source"]["diPointY"]/100);
            if(isset($temp[$coord_key])) {
                $p = array(
                    $item["_id"],
                    $item["_source"]['name'], 
                    $item["_source"]['addr'],
                    $item["_source"]['diPointX'],
                    $item["_source"]['diPointY'],
                    "geo",
                    @implode("\t", $item["_source"]["baidu_geo"]),
                );
                echo implode("\t", $p), "\n";
                $item["_source"]['baidu_geo'] = array_values($temp[$coord_key]);
                $body .= '{ "update": { "_index": "'.$index.'", "_type": "'.$item['_type'].'", "_id": "'.$item['_id'].'" }}';
                $body .= "\n";
                $body .= '{"doc": '.json_encode($item["_source"]).'}';
                $body .= "\n";
            }
        }
        if ($body) {
            $update_res = fly_curl($conf['es']['hosts'].'/_bulk',$body);
            $success = substr_count($update_res, '"successful"');
            echo "更新 [",$index,"] 索引 ...", "目标写入", count($page["hits"]["hits"]),"条数据,","成功写入",$success,"条数据";
        }
    }
    
    
} catch(Exception $e) {
    echo '<pre>';
    var_dump($e->getCode(), $e->getLine(), $e->getMessage());
    echo '</pre>';
}


